/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2025 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.axelor.apps.budget.service;

import com.axelor.apps.base.service.CurrencyScaleService;
import com.axelor.apps.budget.db.Budget;
import com.axelor.apps.budget.db.BudgetVersion;
import com.axelor.apps.budget.db.GlobalBudget;
import com.axelor.apps.budget.db.VersionExpectedAmountsLine;
import com.axelor.apps.budget.db.repo.BudgetVersionRepository;
import com.axelor.apps.budget.db.repo.VersionExpectedAmountsLineRepository;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.math.BigDecimal;
import java.util.List;

public class BudgetVersionServiceImpl implements BudgetVersionService {

  protected BudgetVersionRepository budgetVersionRepository;
  protected VersionExpectedAmountsLineRepository versionExpectedAmountsLineRepository;
  protected CurrencyScaleService currencyScaleService;

  @Inject
  public BudgetVersionServiceImpl(
      BudgetVersionRepository budgetVersionRepository,
      VersionExpectedAmountsLineRepository versionExpectedAmountsLineRepository,
      CurrencyScaleService currencyScaleService) {
    this.budgetVersionRepository = budgetVersionRepository;
    this.versionExpectedAmountsLineRepository = versionExpectedAmountsLineRepository;
    this.currencyScaleService = currencyScaleService;
  }

  @Override
  @Transactional
  public BudgetVersion createNewVersion(GlobalBudget globalBudget, String name) {
    BudgetVersion budgetVersion = new BudgetVersion();
    budgetVersion.setGlobalBudget(globalBudget);
    budgetVersion.setName(name);
    budgetVersion.setCode(globalBudget.getCode());
    budgetVersion.setIsActive(false);

    List<Budget> budgets = globalBudget.getBudgetList();
    BigDecimal globalExpectedAmount = BigDecimal.ZERO;

    for (Budget budget : budgets) {
      VersionExpectedAmountsLine versionExpectedAmountsLine = new VersionExpectedAmountsLine();
      versionExpectedAmountsLine.setBudget(budget);
      versionExpectedAmountsLine.setExpectedAmount(
          currencyScaleService.getCompanyScaledValue(budget, budget.getTotalAmountExpected()));
      globalExpectedAmount =
          currencyScaleService.getCompanyScaledValue(
              budget, globalExpectedAmount.add(budget.getTotalAmountExpected()));
      budgetVersion.addVersionExpectedAmountsLineListItem(versionExpectedAmountsLine);
    }

    globalBudget.addBudgetVersionListItem(budgetVersion);

    budgetVersion = budgetVersionRepository.save(budgetVersion);

    return budgetVersion;
  }
}
